package com.lagou.controller;

import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {

        List<Role> allRole = roleService.findAllRole(role);

        return new ResponseResult(true, 200, "查询成功", allRole);

    }

    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role) {

        if (role.getId() == null) {
            roleService.saveRole(role);
            return new ResponseResult(true, 200, "新增成功", " ");
        } else {
            roleService.updateRole(role);
            return new ResponseResult(true, 200, "修改成功", " ");
        }
    }

    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        List<Menu> parentMenuList = roleService.findSubMenuListByPid(-1);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("parentMenuList", parentMenuList);
        return new ResponseResult(true, 200, "查询成功", parentMenuList);
    }

    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {

        List<String> menuList = roleService.findMenuByRoleId(roleId);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", menuList);
        return result;

    }

    @RequestMapping("/RoleContextMenu")
    public ResponseResult roleContextMenu(@RequestBody RoleMenuVo roleMenuVo){

        roleService.RoleContextMenu(roleMenuVo);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", "");
        return result;

    }

    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(Integer id){
        roleService.deleteRole(id);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", "");
        return result;
    }

    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId){
        List<ResourceCategory> list = roleService.findResourceListByRoleId(roleId);

        return new ResponseResult(true,200,"获取当前角色拥有的资源信息成功",list);

    }

    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVo roleResourceVo){
        roleService.roleContextResource(roleResourceVo);
        return new ResponseResult(true,200,"分配资源成功",null);
    }

}
